import clientFunc from "./client.func"
import ClientInterface from "./ClientInterface"
import CollectionInterfaceBuilder from "./CollectionInterfaceBuilder"
import CollectionRegistration from "./CollectionRegistration"
import CreateSensorRoutine from "./CreateSensorRoutine"
import CreateSourceRoutine from "./CreateSourceRoutine"
import DisconnectMessage from "./DisconnectMessage"
import EnumInterfaceBuilder from "./EnumInterfaceBuilder"
import EventInterfaceBuilder from "./EventInterfaceBuilder"
import FunctionInterfaceBuilder from "./FunctionInterfaceBuilder"
import FunctionRegistration from "./FunctionRegistration"
import InjectRoutine from "./InjectRoutine"
import ObservableInterfaceBuilder from "./ObservableInterfaceBuilder"
import ObservableRegistration from "./ObservableRegistration"
import RoomArrayFactory from "./RoomArrayFactory"
import RoomFactory from "./RoomFactory"

class RegistrationHelper {
  sdkBuilder: SdkBuilder
  constructor(t: SdkBuilder) {
    this.sdkBuilder = t
  }
  get functionRegister() {
    return this.sdkBuilder.functionRegistration
  }
  get observableRegister() {
    return this.sdkBuilder.observableRegistration
  }
  get collectionRegister() {
    return this.sdkBuilder.collectionRegistration
  }
}
class SdkBuilder {
  functionRegistration: FunctionRegistration
  observableRegistration: ObservableRegistration
  collectionRegistration: CollectionRegistration
  messenger: any
  clientFcns: ClientInterface
  enums: EnumInterfaceBuilder
  eventHandler: EventInterfaceBuilder
  functions: FunctionInterfaceBuilder
  observable: ObservableInterfaceBuilder
  collections: any
  constructor(t) {
    this.messenger = t.createMessenger()
    this.functionRegistration = new FunctionRegistration(this.messenger)
    this.observableRegistration = new ObservableRegistration(this.messenger)
    this.collectionRegistration = new CollectionRegistration(this.messenger)
    const e = new RegistrationHelper(this)
    const s = [InjectRoutine, CreateSensorRoutine, CreateSourceRoutine]
    this.functionRegistration.initRoutines(e, ...s)
    const i = [clientFunc.lt("current.room", RoomArrayFactory)]
    this.observableRegistration.initObjectFactories(e, ...i)
    const r = [clientFunc.ut("collection.room", RoomFactory)]
    this.collectionRegistration.initElementFactories(e, ...r)
    this.clientFcns = new ClientInterface()
    this.enums = new EnumInterfaceBuilder()
    this.eventHandler = new EventInterfaceBuilder(this.messenger)
    this.functions = new FunctionInterfaceBuilder(this.functionRegistration)
    this.observable = new ObservableInterfaceBuilder(this.observableRegistration)
    this.collections = new CollectionInterfaceBuilder(this.collectionRegistration)
    this.messenger.init()
  }
  build(t) {
    //构建connect返回的SDK对象并返回
    const e: any = {}
    const s = this.eventHandler.build()
    e.on = (t, i) => (s.on(t, i), e)
    e.off = (t, i) => (s.off(t, i), e)
    const i = [
      [this.enums, t.enums],
      [this.functions, t.functions],
      [this.observable, t.observables],
      [this.collections, t.collections]
    ]
    for (const t of i)
      if (t[1]) {
        clientFunc.dt(t[0].build(t[1]), e)
      }
    clientFunc.dt(this.clientFcns.build(), e)
    e.disconnect = () => {
      e.disconnect = () => {}
      this.dispose()
    }
    return e
  }
  dispose() {
    this.enums.dispose(),
      this.eventHandler.dispose(),
      this.functionRegistration.dispose(),
      this.observableRegistration.dispose(),
      this.collectionRegistration.dispose(),
      this.messenger.send(new DisconnectMessage()),
      this.messenger.dispose()
  }
}
window["sdk-client"] = { SdkBuilder }
export default { SdkBuilder }
